﻿Imports System.ServiceModel.DomainServices.Client
Imports LullabyFullDeCaixa.Web

Partial Public Class FullDeCaixaTancament
    Inherits ChildWindow

    Private _LLibreCaixa As Web.LLibreCaixa
    Public ReadOnly Property LLibreCaixa As Web.LLibreCaixa
        Get
            Return _LLibreCaixa
        End Get
    End Property


#Region "Crear i Destruir"
    Public Sub New(pLLibreCaixa As Web.LLibreCaixa)
        MyBase.New()

        InitializeComponent()
        _LLibreCaixa = pLLibreCaixa
    End Sub


    Private Sub Child_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, True)
    End Sub

    Private Sub FullDeCaixaAlta_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If RiaContext.Current.Authentication.User.Identity.IsAuthenticated = True Then

            If Not _LLibreCaixa.CaixaFinal.HasValue Then _LLibreCaixa.CaixaFinal = 0
            If Not _LLibreCaixa.Sobre.HasValue Then _LLibreCaixa.Sobre = 0

            If _LLibreCaixa.EsTancat Then Me.btnFullDeCaixaTancar.IsEnabled = False

            Me.gridDetall.DataContext = _LLibreCaixa

            Me.cboEstatRecepcioInici.ItemsSource = OpcionsComboEstatHostal.GetOpcions
            Me.cboEstatBanyInici.ItemsSource = OpcionsComboEstatHostal.GetOpcions
            Me.cboEstatCuinaInici.ItemsSource = OpcionsComboEstatHostal.GetOpcions
            Me.cboEstatPassadisInici.ItemsSource = OpcionsComboEstatHostal.GetOpcions
            Me.cboEstatSalaInici.ItemsSource = OpcionsComboEstatHostal.GetOpcions
            Me.cboEstatTerrassaInici.ItemsSource = OpcionsComboEstatHostal.GetOpcions

            Me.cboEstatRecepcioFinal.ItemsSource = OpcionsComboEstatHostal.GetOpcions
            Me.cboEstatBanyFinal.ItemsSource = OpcionsComboEstatHostal.GetOpcions
            Me.cboEstatCuinaFinal.ItemsSource = OpcionsComboEstatHostal.GetOpcions
            Me.cboEstatPassadisFinal.ItemsSource = OpcionsComboEstatHostal.GetOpcions
            Me.cboEstatSalaFinal.ItemsSource = OpcionsComboEstatHostal.GetOpcions
            Me.cboEstatTerrassaFinal.ItemsSource = OpcionsComboEstatHostal.GetOpcions

        End If
    End Sub
#End Region

#Region "Guardar i Cancelar"
    Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        EliminarObjectErrors()

        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            If ValidarDades() And Validacio.Errors.Count = 0 Then
                LullabyFullDeCaixa.Contexte.DomainContext.SubmitChanges(AddressOf DomainContext_SubmittedChanges, Nothing)
                Activity.IsActive = True
            End If
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Function ValidarDades() As Boolean
        Dim Resultat As Boolean = True

        Return Resultat
    End Function

    Private Function ValidarDadesPerTancar() As Boolean
        Dim Resultat As Boolean = True

        Resultat = ValidarDades()

        If Not _LLibreCaixa.EstatRecepcioInici.HasValue Or Not _LLibreCaixa.EstatRecepcioFinal.HasValue Then
            Me.Validacio.Errors.Add(New ValidationSummaryItem(StringLibrary.EstatRecepcioNoValid))
            Resultat = False
        End If

        If Not _LLibreCaixa.EstatBanyInici.HasValue Or Not _LLibreCaixa.EstatBanyFinal.HasValue Then
            Me.Validacio.Errors.Add(New ValidationSummaryItem(StringLibrary.EstatBanyNoValid))
            Resultat = False
        End If

        If Not _LLibreCaixa.EstatCuinaInici.HasValue Or Not _LLibreCaixa.EstatCuinaFinal.HasValue Then
            Me.Validacio.Errors.Add(New ValidationSummaryItem(StringLibrary.EstatCuinaNoValid))
            Resultat = False
        End If

        If Not _LLibreCaixa.EstatSalaInici.HasValue Or Not _LLibreCaixa.EstatSalaFinal.HasValue Then
            Me.Validacio.Errors.Add(New ValidationSummaryItem(StringLibrary.EstatSalaNoValid))
            Resultat = False
        End If

        If Not _LLibreCaixa.EstatPassadisInici.HasValue Or Not _LLibreCaixa.EstatPassadisFinal.HasValue Then
            Me.Validacio.Errors.Add(New ValidationSummaryItem(StringLibrary.EstatPassadisNoValid))
            Resultat = False
        End If

        If Not _LLibreCaixa.EstatTerrassaInici.HasValue Or Not _LLibreCaixa.EstatTerrassaFinal.HasValue Then
            Me.Validacio.Errors.Add(New ValidationSummaryItem(StringLibrary.EstatTerrassaNoValid))
            Resultat = False
        End If

        Return Resultat
    End Function

    Private Sub DomainContext_SubmittedChanges(ByVal s As SubmitOperation)
        Activity.IsActive = False

        If s.HasError Then
            For Each E As Entity In s.EntitiesInError
                For Each VError As ComponentModel.DataAnnotations.ValidationResult In E.ValidationErrors
                    Me.Validacio.Errors.Add(New ValidationSummaryItem(VError.ErrorMessage))
                Next
            Next
            s.MarkErrorAsHandled()
            'App.ReportError(StringLibrary.ErrorGuardantDades)
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            LullabyFullDeCaixa.Contexte.DomainContext.RejectChanges()
        End If

        Me.DialogResult = False
    End Sub

    Public Sub EliminarObjectErrors()
        Dim i As Long

        i = 0
        While i < Validacio.Errors.Count
            If Validacio.Errors(i).ItemType = ValidationSummaryItemType.ObjectError Then
                Validacio.Errors.RemoveAt(i)
            Else
                i = i + 1
            End If
        End While
    End Sub

    Private Sub btnFullDeCaixaTancar_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        EliminarObjectErrors()

        If ValidarDadesPerTancar() And Validacio.Errors.Count = 0 Then
            Dim inputMessage As InputWindow = New InputWindow(StringLibrary.TancamentConfirm, InputWindow.EnumTipus.PasswordBox)

            AddHandler inputMessage.Closed, AddressOf VerificarTancament
            inputMessage.Show()
        End If
    End Sub

    Private Sub VerificarTancament(sender As System.Object, e As System.EventArgs)
        RemoveHandler DirectCast(sender, InputWindow).Closed, AddressOf VerificarTancament

        If DirectCast(sender, InputWindow).DialogResult.HasValue AndAlso DirectCast(sender, InputWindow).DialogResult Then
            If DirectCast(sender, InputWindow).Input = Contexte.UsuariActual.Password Then
                LLibreCaixa.DataHoraTancament = Date.SpecifyKind(Now, DateTimeKind.Unspecified)

                LullabyFullDeCaixa.Contexte.DomainContext.SubmitChanges(AddressOf DomainContext_SubmittedChanges, Nothing)
                Activity.IsActive = True
            Else
                MessageBox.Show(StringLibrary.PasswordIncorrecte & vbNewLine, StringLibrary.ErrorTancament, MessageBoxButton.OK)
            End If
        End If
    End Sub
#End Region



End Class
